import {reqGetOrderInfo, reqGetOrderListByUserId, reqGetOnlineOrderList} from "@/api";

const state = {
    orderInfo:[],
    orderInfo_one:[],
    orderListOfUser:[],
    onlineList: [],
}
const actions = {
    // 根据orderId获取订单信息
    async getOrderInfo({commit}, config){
        let result = await reqGetOrderInfo(config.send);
        if (result.code=200){
            console.log('orderData---getOrderInfo数据获取成功！');
            switch (config.type){
                case 0:
                    commit('GETORDERINFO', result.data);
                    console.log('执行了case 0')
                    break;
                case 1:
                    commit('GETORDERINFO_ONE', result.data);
                    console.log('执行了case 1')
                    break;
            }
        }else{
            this._vm.$message({
                showClose:true,
                message:'orderData---getOrderInfo数据获取失败！',
                type:"error"
            })
        }
    },
    // 根据userId获取订单列表
    async getOrderListByUserId({commit}, config){
        let result = await reqGetOrderListByUserId(config);
        if (result.code=200){
            console.log('仓库orderData——orderListOfUser获取数据成功！');
            commit('GETORDERLISTBYUSERID', result.data);
        }else{
            this._vm.$message({
                showClose:true,
                message:'仓库orderData——orderListOfUser获取数据失败！',
                type:"error"
            });
        }
    },
    // 获取线上待分配订单
    async getOnlineOrder({commit}){
        let result = await reqGetOnlineOrderList();
        if (result.code=200){
            console.log('仓库orderData——getOnlineOrder获取数据成功！');
            commit('GETONLINEORDER', result.data);
        }else{
            this._vm.$message({
                showClose:true,
                message:'仓库orderData——getOnlineOrder获取数据失败！',
                type:"error"
            });
        }
    }
}
const mutations = {
    GETORDERINFO(state, data){
        state.orderInfo = data;
    },
    GETORDERINFO_ONE(state, data){
        state.orderInfo_one = data;
    },
    GETORDERLISTBYUSERID(state, data){
        state.orderListOfUser = data;
    },
    GETONLINEORDER(state, data){
        state.onlineList = data;
    }
}
const getters = {
    // 返回根据orderId返回的订单信息
    getOrderInfoByOrderId(){
        return state.orderInfo;
    },
    // 返回根据orderId返回的订单信息
    getOrderInfoByOrderIdOne(){
        return state.orderInfo_one;
    },
    getOrderListOfUser(){
        return state.orderListOfUser || [];
    },
    getOnlineOrderList(){
        return state.onlineList || [];
    },
    // 只返回订单id列表
    getOnlineOrderListLocalInfo(){
        let newArr = [];
        state.onlineList.forEach(item=>{
            newArr.push(Object.assign({}, {
                orderId:item.orderId+'',
                value:item.orderId+''
            }))
        })
        return newArr || [];
    }
}

export default {
    state,
    actions,
    mutations,
    getters
}